use axum::{
    extract::Request,
    http::{HeaderMap, HeaderValue, StatusCode},
    middleware::Next,
    response::Response,
};

pub async fn auth(
    headers: HeaderMap,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    match headers.get("token") {
        Some(token) if token_is_valid(token) => {
            println!("===> bef next");
            let response = next.run(request).await;
            println!("===> aft next");
            Ok(response)
        }
        _ => Err(StatusCode::UNAUTHORIZED),
    }
}

fn token_is_valid(token: &HeaderValue) -> bool {
    token == "access_token"
}
